using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Duckbill
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        public const int SCREEN_WIDTH = 640;
        public const int SCREEN_HEIGHT = 480;
        public static Vector2 SCREEN = new Vector2(SCREEN_WIDTH, SCREEN_HEIGHT);

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public static SpriteFont font;
        public static StateManager states = new StateManager();
        public static Map map;

        public static Player player;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = SCREEN_WIDTH;
            graphics.PreferredBackBufferHeight = SCREEN_HEIGHT;
            graphics.ApplyChanges();
            this.Window.AllowUserResizing = true;
            Window.ClientSizeChanged += new EventHandler<System.EventArgs>(Window_ClientSizeChanged);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            states.Push(new TitleState());
        }

        public static void StartGame()
        {
            player = new Player();
            map = new Map();
            TransitionState T = new TransitionState(states.Top(), new GameState(Game.map.GetRoom(0)));
            states.Pop(); //remove LoadingState
            states.Push(T);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            GameObject.LoadSprites(this.Content);
            font = this.Content.Load<SpriteFont>("DerpFont");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            float t = (float)gameTime.ElapsedGameTime.Milliseconds/1000;
            states.Update(t);

            base.Update(gameTime);
        }

        // Make changes to handle the new window size.            
        void Window_ClientSizeChanged(object sender, EventArgs e)
        {
            Sprite.scale.X = (float)this.Window.ClientBounds.Width / SCREEN_WIDTH;
            Sprite.scale.Y = (float)this.Window.ClientBounds.Height / SCREEN_HEIGHT;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(new Color(0, 30, 141, 255));

            //GraphicsDevice.DepthStencilState.Depth
            
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.PointClamp, null, null);
            states.Draw(spriteBatch);
            spriteBatch.End();
            
            base.Draw(gameTime);
        }
    }
}
